package cn.itsource.ldj.org.controller;

import cn.itsource.ldj.org.domain.Shoplog;
import cn.itsource.ldj.org.domain.Shop;
import cn.itsource.ldj.org.query.ShopQuery;
import cn.itsource.ldj.org.service.IShopService;
import cn.itsource.ldj.util.AjaxResult;
import cn.itsource.ldj.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin 解决跨域问题，添加cors头，不建议用，
@RestController // 复合注解，相当于@Controller和ResponseBody这两个注解
@RequestMapping("/shop") // 请求后台地址
public class ShopController {

    @Autowired
    private IShopService shopService;

    /* 添加或者修改方法 */
    @PutMapping // 必须是put请求才会进入这个方法
    public AjaxResult saveOrUpdate(@RequestBody Shop shop) { // @RequestBody注解将接收的json数据转换成对象接收
        try {
            /* 如果传入的对象有id，执行修改方法，否则执行添加方法 */
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.save(shop);
                System.out.println(shop.getRegisterTime());
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    /* 删除方法 */
    @DeleteMapping("/{id}") // 必须为delete请求才会进入方法，"/{id}"为占位符
    public AjaxResult delete(@PathVariable("id") Long id) { // @PathVariable注解将请求中的携带的占位符获取出来传入参数
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /* 查询单个对象 */
    @GetMapping("/{id}") // 必须为get请求才会进入方法，"/{id}"为占位符
    public Shop findOne(@PathVariable("id") Long id) { // @PathVariable注解将请求中的携带的占位符获取出来传入参数
        try {
            return shopService.findOne(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /* 查询所有对象 */
    @GetMapping // 必须以get请求进入这个查询全部方法
    public List<Shop> findAll() {
        try {
            System.out.println("111");
            return shopService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /* 分页查询和高级查询 */
    @PostMapping
    public QueryResult<Shop> queryPage(@RequestBody ShopQuery query) {
        QueryResult<Shop> listData = shopService.queryPage(query);
        return listData;
    }


    /* 批量删除 */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) { // @RequestBody注解将请求中的携带的占位符获取出来传入参数
        try {
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }


    /* 商家入驻 */
    @PostMapping("/settlement")
    public AjaxResult recruitment(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
    }

    /* 驳回 */
    @PostMapping("/noexamine")
    public AjaxResult examine(@RequestBody Shoplog shoplog) {
        try {
            shopService.noexamine(shoplog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }


    /* 驳回修改后 */
    @PostMapping("/amend")
    public AjaxResult amend(@RequestBody Shop shop) {
        try {
            System.out.println(shop);
            shop.setState(0);
            shopService.update(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    /* 审核通过 */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody Shoplog shoplog) {
        try {
            shopService.pass(shoplog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    /* 激活 */
    @RequestMapping("/activate")
    public void activate(Long id) {
        shopService.activate(id);
    }
}
